package com.hsytech.baoinbao.unwaste;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.hsytech.baoinbao.R;
import com.hsytech.baoinbao.SharedPreferencesTools;
import com.hsytech.baoinbao.base.BaseActivity;
import com.hsytech.baoinbao.volleytool.ApiUrls;
import com.hsytech.baoinbao.volleytool.HttpUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 123 on 2016/3/15.
 */
public class WasteUnStorageListActivity extends BaseActivity {
    private GridView mGrid_view;
    private MGridViewAdapter mGridViewAdapter;
    private List<HashMap<String, String>> listData;
    private Intent intent;
    private String area_id, area_name;
    private HttpUtil.HttpCallback httpCallback = new HttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, JSONObject response) {
            JSONObject result = response.optJSONObject("result");
            JSONArray list = result.optJSONArray("list");
            Log.d("mylog", result.toString());
            if (result.toString().equals("[]") || list.length() == 0) {
                showToast("此区域无订单");
                listData.clear();
            } else {
                for (int i = 0; i < list.length(); i++) {
                    String code = list.optJSONObject(i).optString("code");
                    String order_time = list.optJSONObject(i).optString("order_time");
                    String u_name = list.optJSONObject(i).optString("u_name");
                    String u_user_name = list.optJSONObject(i).optString("u_user_name");
                    String area_name = list.optJSONObject(i).optString("area_name");
                    String r_name = list.optJSONObject(i).optString("r_name");
                    String r_user_name = list.optJSONObject(i).optString("r_user_name");
                    String order_id = list.optJSONObject(i).optString("order_id");

                    HashMap<String, String> hashMap = new HashMap<>();
                    hashMap.put("code", code);
                    hashMap.put("u_name", u_name);
                    hashMap.put("u_user_name", u_user_name);
                    hashMap.put("area_name", area_name);
                    hashMap.put("r_name", r_name);
                    hashMap.put("r_user_name", r_user_name);
                    hashMap.put("order_id", order_id);
                    listData.add(hashMap);
                }

            }
            mGridViewAdapter.notifyDataSetChanged();
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            Log.d("mylog", "onError:" + (null == error ? "" : error.getMessage()));
            showToast(R.string.msg_http_failure);
        }

        @Override
        public void onErrorMsg(int what, String msg) {
            Log.d("mylog", "onErrorMsg msg");
            showToast(msg);
        }
    };

    @Override
    public void onMenuRightBtn() {
        super.onMenuRightBtn();

        if (listData != null && listData.size() != 0) {
            Intent intent = new Intent(WasteUnStorageListActivity.this, WasteUnStorageEasyOutActivity.class);
            intent.putExtra("area_id", area_id);
            intent.putExtra("area_name", area_name);
            Log.d("mylog", "area_id==" + area_id);
            startActivity(intent);
        } else {
            showToast("此区域无订单");
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_un_waste_list);
        setCenterTitleText("订单详情");
        setRightBtn(true);
        setRight_button_text("一键出库");

        mGrid_view = F(R.id.mGrid_view);
        intent = getIntent();
        area_id = intent.getStringExtra("area_id");
        area_name = intent.getStringExtra("area_name");
        listData = new ArrayList<>();

        mGridViewAdapter = new MGridViewAdapter(this, listData);
        mGrid_view.setAdapter(mGridViewAdapter);
        mGrid_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent intent = new Intent(WasteUnStorageListActivity.this, WasteUnStorageListDetailActivity.class);
                intent.putExtra("order_id", listData.get(i).get("order_id"));
                Log.d("mylog", "order_id==" + listData.get(i).get("order_id"));
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        listData.clear();
        mGridViewAdapter.notifyDataSetChanged();
        Map<String, String> data = new HashMap<>();
        HttpUtil.httpGetWithParams(0, ApiUrls.GOODS_ORDER_LIST + "?" + "token=" + SharedPreferencesTools.getTOKEN(WasteUnStorageListActivity.this) + "&" + "area_id" + "=" + area_id, data, httpCallback, WasteUnStorageListActivity.this);
    }

    private class MGridViewAdapter extends BaseAdapter {

        private Context context;
        private List<HashMap<String, String>> listdata;
        private LayoutInflater inflater;

        public MGridViewAdapter(Context context, List<HashMap<String, String>> listdata) {
            this.context = context;
            inflater = LayoutInflater.from(context);
            this.listdata = listdata;
        }

        @Override
        public int getCount() {
            return listdata.size();
        }

        @Override
        public Object getItem(int position) {
            return listdata.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View itemView, ViewGroup parent) {

            itemView = inflater.inflate(R.layout.item_un_waste_list, null);
            TextView code = (TextView) itemView.findViewById(R.id.code);
            TextView u_name = (TextView) itemView.findViewById(R.id.u_name);
            TextView area_name = (TextView) itemView.findViewById(R.id.area_name);
            TextView r_name = (TextView) itemView.findViewById(R.id.r_name);

            HashMap<String, String> map = listdata.get(position);
            code.setText(map.get("code"));
            u_name.setText(map.get("u_name") + "-" + map.get("u_user_name"));
            area_name.setText(map.get("area_name"));
            r_name.setText(map.get("r_name") + "-" + map.get("r_user_name"));
            return itemView;
        }
    }
}
